Utility Methods Class
Various utilities that the CreateJS Suite uses. Utilities are created as separate files, and will be available on the createjs namespace directly.
Example
myObject.addEventListener("change", createjs.proxy(myMethod, scope));
Methods
deprecate
-
[fallbackMethod=null]
-
[name=null]
Wraps deprecated methods so they still be used, but throw warnings to developers.
obj.deprecatedMethod = createjs.deprecate("Old Method Name", obj._fallbackMethod);
The recommended approach for deprecated properties is:
try {
Obj ect.defineProperties(object, {
readyOnlyProp: { get: createjs.deprecate("readOnlyProp", function() { return this.alternateProp; }) },
readWriteProp: {
get: createjs.deprecate("readOnlyProp", function() { return this.alternateProp; }),
set: createjs.deprecate("readOnlyProp", function(val) { this.alternateProp = val; })
});
} catch (e) {}
Parameters:
Returns:
If a fallbackMethod is supplied, returns a closure that will call the fallback method after logging the warning in the console.
extend
-
subclass
-
superclass
Sets up the prototype chain and constructor property for a new class.
This should be called right after creating the class constructor.
function MySubClass() {}
createjs.extend(MySubClass, MySuperClass);
MySubClass.prototype.doSomething = function() { }
var foo = new MySubClass();
console.log(foo instanceof MySuperClass); // true
console.log(foo.prototype.constructor === MySubClass); // true
Returns:
Returns the subclass's new prototype.
indexOf
-
array
-
searchElement
Finds the first occurrence of a specified value searchElement in the passed in array, and returns the index of that value. Returns -1 if value is not found.
var i = createjs.indexOf(myArray, myElementToFind);
Parameters:
Returns:
The first index of searchElement in array.
promote
-
subclass
-
prefix
Promotes any methods on the super class that were overridden, by creating an alias in the format prefix_methodName
.
It is recommended to use the super class's name as the prefix.
An alias to the super class's constructor is always added in the format prefix_constructor
.
This allows the subclass to call super class methods without using function.call
, providing better performance.
For example, if MySubClass
extends MySuperClass
, and both define a draw
method, then calling promote(MySubClass, "MySuperClass")
would add a MySuperClass_constructor
method to MySubClass and promote the draw
method on MySuperClass
to the
prototype of MySubClass
as MySuperClass_draw
.
This should be called after the class's prototype is fully defined.
function ClassA(name) {
this.name = name;
}
ClassA.prototype.greet = function() {
return "Hello "+this.name;
}
function ClassB(name, punctuation) {
this.ClassA_constructor(name);
this.punctuation = punctuation;
}
createjs.extend(ClassB, ClassA);
ClassB.prototype.greet = function() {
return this.ClassA_greet()+this.punctuation;
}
createjs.promote(ClassB, "ClassA");
var foo = new ClassB("World", "!?!");
console.log(foo.greet()); // Hello World!?!
Parameters:
Returns:
Returns the subclass.
proxy
-
method
-
scope
-
[arg]
A function proxy for methods. By default, JavaScript methods do not maintain scope, so passing a method as a callback will result in the method getting called in the scope of the caller. Using a proxy ensures that the method gets called in the correct scope.
Additional arguments can be passed that will be applied to the function when it is called.
Example
myObject.addEventListener("event", createjs.proxy(myHandler, this, arg1, arg2));
function myHandler(arg1, arg2) {
// This gets called when myObject.myCallback is executed.
}